package edu.gatech.ors.mrsim.core.impl;

import java.util.HashMap;

import edu.gatech.ors.mrsim.core.Data;
import edu.gatech.ors.mrsim.core.FileBlock;
import edu.gatech.ors.mrsim.core.SimFile;

public class HDFSDataNode extends GenericNode {
	private int maxNumHandler;
	private int numHandling;
	private int blockSize;
	private HashMap<String, SimFile> files;

	public HDFSDataNode(String ID, int maxHandle, int blockSize, HashMap<String, SimFile> files) {
		super(ID);
		this.maxNumHandler = maxHandle;
		this.blockSize = blockSize;
		this.files = files;
		numHandling = 0;

	}

	public HDFSDataNode(String ID, int maxHandle, int blockSize) {
		this(ID, maxHandle, blockSize, new HashMap<String, SimFile>());
	}

	public boolean respondToRequest(IORequest request) {
		String fileName = request.getFileName();
		boolean isFile = request.getData() instanceof SimFile;
		Data<?> data = request.getData();
		if (request.isWrite()) {
			if (isFile) {
				SimFile file = (SimFile) data;
				files.put(fileName, file);
			} else if (data instanceof FileBlock && files.containsKey(request.getFileName())) {
				SimFile file = files.get(request.getFileName());
				if (file.writeData((FileBlock) data)) {
					request.addDuration((long) (Math.random() * 10 + 5));// TODO: memory time
					return true;
				}
			}
		} else {
			if (files.containsKey(request.getFileName())) {
				SimFile file = files.get(fileName);
				request.setData(file);
				request.addDuration((long) (Math.random() * 10 + 5));// TODO: memory time
				return true;
			}
		}
		return false;
	}

}
